package com.example.yuecvideo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yuecvideo.R;
import com.example.yuecvideo.dao.DBDao;
import com.example.yuecvideo.utils.ConnectUtil;
import com.example.yuecvideo.utils.ToastUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicBoolean;

public class LoginActivity extends AppCompatActivity {

    private Button loginBtn;
    private EditText accountEdit, passwordEdit;
    private CheckBox rememberCbx, autoLoginCbx;
    private SharedPreferences.Editor loginEditor;
    private boolean isSavePsd, isAutoLogin;
    private SharedPreferences loginPreferences;
    private String account, userPsd, userName;

    /// 当在数据库查询到登录的用户时赋值为true,否则为false


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Init();
        doLogin();
        onAutoLogin();
    }


    private void Init() {
        loginBtn = (Button) this.findViewById(R.id.button_Login);
        accountEdit = (EditText) this.findViewById(R.id.userName);
        passwordEdit = (EditText) this.findViewById(R.id.password);
        rememberCbx = (CheckBox) this.findViewById(R.id.rememberPsd);
        autoLoginCbx = (CheckBox) this.findViewById(R.id.autoLogin);


        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        if (bundle != null) {
            accountEdit.setText(bundle.getString("account", null));
            passwordEdit.setText(bundle.getString("password", null));

        }


    }

    void onAutoLogin() {
        loginPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        isSavePsd = loginPreferences.getBoolean("isSavePsd", false);
        isAutoLogin = loginPreferences.getBoolean("isAutoLogin", false);
        account = loginPreferences.getString("userName", null);
        userPsd = loginPreferences.getString("password", null);
        if (isAutoLogin) {
            jumpLoginToHome();
        } else {
            syncCheckBox();

        }
    }

    void doLogin() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
                if (checkIsValid()) {
                    return;
                }
                checkDB();


            }
        });
    }

    void syncCheckBox() {
        if (isSavePsd) {
            accountEdit.setText(account);
            passwordEdit.setText(userPsd);
            rememberCbx.setChecked(true);
        }
        autoLoginCbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    rememberCbx.setChecked(true);
                }
            }
        });
    }

    private boolean checkIsValid() {
        account = accountEdit.getText().toString();
        Context context = getApplicationContext();
        if (ToastUtil.Empty(account, "用户名", context)) {
            return false;
        }
        userPsd = passwordEdit.getText().toString();
        return ToastUtil.Empty(userPsd, "密码", context);
    }

    void checkDB() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String sql = "select userName from user where account = '" + account
                            + "' and password = '" + userPsd + "'";
                    ResultSet resultSet = DBDao.Select(sql);
                    if (resultSet.next()) {
                        userName = resultSet.getString("userName");
                        loginEditor.commit();
                        jumpLoginToHome();
                        return;
                    } else {
                        ToastUtil.ShowText(account + "尚未创建", getApplicationContext());

                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    void login() {
        loginEditor = loginPreferences.edit();
        account = accountEdit.getText().toString();
        userPsd = passwordEdit.getText().toString();
        loginEditor.putString("userName", account);
        loginEditor.putString("password", userPsd);
        loginEditor.putBoolean("isSavePsd", rememberCbx.isChecked());
        loginEditor.putBoolean("isAutoLogin", autoLoginCbx.isChecked());

    }


    private void jumpLoginToHome() {
        Intent in = new Intent(LoginActivity.this, HomePageActivity.class);
        in.putExtra("account", account);      // 将用户名密码传入Mundle，方便在切换页面时读取数据
        in.putExtra("password", userPsd);
        in.putExtra("userName", userName);
        startActivity(in);
    }


}